package com.runen.wnhz.runen.ui.activity.mine;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.alipay.AlipayTools;
import com.runen.wnhz.runen.common.aspectj.CheckNet;
import com.runen.wnhz.runen.common.dialog.BaseDialog;
import com.runen.wnhz.runen.common.login.Wx.WeiXinPayBean;
import com.runen.wnhz.runen.common.login.Wx.WeiXinUtils;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.Base64Utils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.RechargeBean;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerPayComponent;
import com.runen.wnhz.runen.di.module.PayModule;
import com.runen.wnhz.runen.presenter.Contart.PayContract;
import com.runen.wnhz.runen.presenter.iPresenter.IPayPersenter;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.widget.TitleBuilder;

/**
 * 描述： 支付界面
 * 作者： 天天童话丶
 * 时间： 2018/6/2.
 */

public class PayActivity extends BaseActivity<IPayPersenter> implements PayContract.View {

    //用户
    private UserBean mReqeustUser;

    private BaseDialog mPayDialog;

    private int mPayType = 2;           // 支付类型 1 - 充值 2 - 支付

    //支付
    private String mOrderNo;           //支付订单号
    //充值
    private String mRid;
    private String mRechargeType = "1";  // 1 - 自定义 2 - 系统定义
    private String mPayChannel = "2";       //充值途径 1-支付宝 2-微信
    private String mMoney;

    @Override
    protected int getLayoutView() {
        return R.layout.activity_pay;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {

    }

    @Override
    protected void initStart() {
        mPayType = getIntent().getIntExtra("PayType", 2);
        if (mPayType == 2){
            // 支付类型
            mOrderNo = getIntent().getStringExtra("OrderNo");
        } else {
            // 充值
            mRechargeType = getIntent().getStringExtra("RechargeType");
            mRid = getIntent().getStringExtra("Rid");
            mMoney = getIntent().getStringExtra("RechargeMoney");
        }

        //调用第三方接口
        if (mPayDialog == null) {
            mPayDialog = new BaseDialog.Builder(this)
                    .setContentView(R.layout.dialog_pay)
                    .fullWidth()
                    .fromBottom(true)
                    .create();

            ((TextView) mPayDialog.getView(R.id.dialog_pay_price_tv)).setText(mMoney);

            ((RadioGroup) mPayDialog.getView(R.id.dialog_pay_type_rg)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                public void onCheckedChanged(RadioGroup arg0, int arg1) {
                    int radioButtonId = arg0.getCheckedRadioButtonId();
                    if (radioButtonId == R.id.dialog_pay_type_wx_rb) {
                        mPayChannel = "2";
                    } else if (radioButtonId == R.id.dialog_pay_type_ali_rb) {
                        mPayChannel = "1";
                    }
                }
            });

            mPayDialog.getView(R.id.dialog_pay_tv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mPayType == 2){
                        //支付
                        requestPay();
                    } else {
                        //充值
                        reqeustRecharge();
                    }
                }
            });

            mPayDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    close();
                }
            });
        }

        mPayDialog.show();
    }

    public void close(){
        finish();
        overridePendingTransition(0, 0);
    }

    @CheckNet
    public void reqeustRecharge() {
        mPresenter.reqeustRecharge();
    }

    @CheckNet
    public void requestPay() {
        mPresenter.reqeustPay();
    }

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        mReqeustUser = ACacheUtlis.getInstance().getReqeustUser(this);
    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerPayComponent.builder().applicationComponent(appComponent)
                .payModule(new PayModule(this))
                .build().initject(this);
    }

    @Override
    public String getToken() {
        return mReqeustUser.getToken();
    }

    @Override
    public String getRid() {
        return mRid;
    }

    @Override
    public String getPayChannel() {
        return mPayChannel;
    }

    @Override
    public String getAmount() {
        return Base64Utils.encode(mMoney);
    }

    @Override
    public String getRechargeType() {
        return mRechargeType;
    }

    @Override
    public String getOrderNo() {
        return mOrderNo;
    }

    @Override
    public void onSuccess(RechargeBean rechargeBean) {
        if (TextUtils.equals(mPayChannel, "1")) {
            new AlipayTools(this, new AlipayTools.PayResultListner() {
                @Override
                public void onAlipayResult(int result, String msg) {
                    switch (result) {
                        case 1:
                        case 3:
                            ToastUtil.showToast(msg);
                            break;
                        default:
                            ToastUtil.showToast("支付失败");
                    }
                }
            }).payV2(rechargeBean.getAli_info());
        } else {
            WeiXinUtils.getInstance().wxPay(new WeiXinPayBean(
                    rechargeBean.getRinfo().getAppid(),
                    rechargeBean.getRinfo().getNoncestr(),
                    rechargeBean.getRinfo().getPackageX(),
                    rechargeBean.getRinfo().getPartnerid(),
                    rechargeBean.getRinfo().getPrepayid(),
                    Integer.parseInt(rechargeBean.getRinfo().getTimestamp()),
                    rechargeBean.getRinfo().getSign()
            ));
        }
    }

    @Override
    public void onError(String msg) {
        ToastUtil.showToast(msg);
    }

}
